package com.zhangxx.inventory.controller;

import com.zhangxx.common.exception.ExceptionCast;
import com.zhangxx.common.response.ResponseData;
import com.zhangxx.common.response.ResponseResult;
import com.zhangxx.common.response.resultCode.CommonCode;
import com.zhangxx.inventory.pojo.ProductInventory;
import com.zhangxx.inventory.service.ProductInventoryService;
import com.zhangxx.inventory.thread.asyncProcessService.RequestAsyncProcessService;
import com.zhangxx.inventory.thread.request.ProductInventoryCacheRefreshRequest;
import com.zhangxx.inventory.thread.request.ProductInventoryDBUpdateRequest;
import com.zhangxx.inventory.thread.request.Request;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
 * @ClassName ProductInventoryController
 * @Description 商品信息api
 * @Author leizhang
 * Date 2022/3/18 20:16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/productInventoryApi")
@Slf4j
public class ProductInventoryController {
    @Resource
    private RequestAsyncProcessService requestAsyncProcessService;

    @Resource
    private ProductInventoryService productInventoryService;

    @GetMapping("/test")
    public ResponseResult test(){
        return new ResponseResult(CommonCode.SUCCESS);
    }
    /**
     * @param productInventory
     * @return com.zhangxx.common.response.ResponseData
     * @Description : 商品信息查询
     * @Author : leizhang
     * @Date 20:21 2022/3/18
     **/
    @GetMapping("/findProductInventoryInfo")
    public ResponseData findProductInventoryInfo( ProductInventory productInventory) {
        Request request = new ProductInventoryCacheRefreshRequest(productInventory, productInventoryService, false);
        requestAsyncProcessService.process(request);
        long startTime = System.currentTimeMillis();
        long endTime = 0L;
        long waitTime = 0L;
        try {
            for (; ; ) {
                //等待时间过长退出等待
                if (waitTime > 200) {
                    break;
                }
                //读取redis中缓存数据
                ProductInventory productInventoryOfCache = productInventoryService.findProductInventoryOfCache(productInventory);
                if (null != productInventoryOfCache) {
                    return new ResponseData(productInventoryOfCache, CommonCode.SUCCESS);
                } else {
                    //如果没有读取到结果，那就等会
                    Thread.sleep(20);
                    endTime = System.currentTimeMillis();
                    waitTime = endTime - startTime;
                }
            }
            //时间超时 从数据库中获取数据
            ProductInventory productInventory1 = productInventoryService.findProductInventory(productInventory);
            if (productInventory1 != null) {
                //再次刷新缓存
                request = new ProductInventoryCacheRefreshRequest(productInventory1, productInventoryService, true);
                requestAsyncProcessService.process(request);
                return new ResponseData(productInventory1, CommonCode.SUCCESS);
            }
        } catch (InterruptedException e) {
            log.error("获取库存信息异常:{}", e.getMessage());
        }
        return new ResponseData<>(null, CommonCode.FAIL);
    }

    /**
     * @param
     * @return com.zhangxx.common.response.ResponseResult
     * @Description : 更新库存
     * @Author : leizhang
     * @Date 21:02 2022/3/18
     **/
    @PostMapping("/updateProductInventory")
    public ResponseResult updateProductInventory(@RequestBody ProductInventory productInventory) {
        ResponseResult response;
        try {
//            ResponseData productInventoryInfo = this.findProductInventoryInfo(productInventory);
//            if (null == productInventory || null == productInventoryInfo.getData()) {
//                //抛出异常 商品不存在
//                ExceptionCast.cast(CommonCode.FAIL);
//            }
//            //库存数量小于可以扣减的数量，商品数量不足
//            ProductInventory data = (ProductInventory) productInventoryInfo.getData();
//            if (productInventory.getDelCount() > data.getAvailableCount()) {
//                ExceptionCast.cast(CommonCode.INVENTORY_COUNT_LOW);
//            }
            Request request = new ProductInventoryDBUpdateRequest(productInventory, productInventoryService);
            requestAsyncProcessService.process(request);
            response = new ResponseResult(CommonCode.SUCCESS);
        }catch (Exception e){
            response = new ResponseResult(CommonCode.FAIL);
            log.error("库存更新失败：{}",e.getMessage());
        }
        return response;
    }
}
